﻿using System;
using System.ComponentModel;
using System.Data.Common;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreateEntity
{
    public partial class MainForm : Form
    {
        internal Color _color = Color.Black;
        internal Font _font = null;
        public MainForm()
        {
            InitializeComponent();
            comboBox1.DataSource = Enum.GetNames(typeof(DataBaseType));
            _font = this.button3.Font;
            _color = this.button3.ForeColor;
            Win32Paint.SetFormRoundRectRgn(this, 7);	//设置圆角
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formMain_Paint(object sender, PaintEventArgs e)
        {
            //this.BackColor = ColorTranslator.FromHtml("#66CCCC");
        }

        //开始按钮
        private void button1_Click(object sender, EventArgs e)
        {
            Helper.nameSpace = NameSpace.Text.Trim();
            Helper.path = txtPath.Text.Trim();
            Helper.conStr = ConStr.Text.Trim();
            Helper.DbContextName = textDbContext.Text.Trim();

            #region 验证
            if (string.IsNullOrEmpty(Helper.nameSpace))
            {
                MessageBox.Show("请填写命名空间", Helper.lable, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(Helper.conStr))
            {
                MyMessageBox.Show("请填写连接字符串");
                return;
            }
            if (string.IsNullOrEmpty(Helper.path))
            {
                MyMessageBox.Show("请选择生成路径");
                return;
            }
            if (Helper.IsDbContext && string.IsNullOrEmpty(Helper.DbContextName))
            {
                MyMessageBox.Show("请输入上下文名称");
                return;
            }
            if (!testCon())
            {
                MyMessageBox.Show("连接失败!");
                return;
            }
            #endregion

            //隐藏自己
            Hide();
            ProgressForm pgForm = new ProgressForm();
            pgForm.ShowDialog();
            Visible = true;
        }


        //选择路径
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            txtPath.Text = path.SelectedPath;
        }

        /// <summary>
        /// 测试连接是否成功
        /// </summary>
        /// <returns>true为成功，false为失败</returns>
        private bool testCon()
        {
            bool ok = true;
            try
            {
                DbConnection conn = Helper.GetDbConnection();
                conn.Open();
                conn.Close();
            }
            catch (Exception e)
            {
                ok = false;
                MyMessageBox.Show("连接超时，" + e.Message);
            }
            return ok;
        }

        private void testConBtn_Click(object sender, EventArgs e)
        {
            Helper.conStr = ConStr.Text.Trim();
            if (testCon())
            {
                MyMessageBox.Show("测试连接成功");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Helper.dbType = (DataBaseType)(Enum.Parse(typeof(DataBaseType), comboBox1.Text, true));
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
            Environment.Exit(0);
        }

        private void checkReplace_CheckedChanged(object sender, EventArgs e)
        {
            Helper.IsReplace = checkReplace.Checked;
        }

        private void checkDbContext_CheckedChanged(object sender, EventArgs e)
        {
            Helper.IsDbContext = checkDbContext.Checked;
            textDbContext.Enabled= checkDbContext.Checked;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MyMessageBox.Show("确定退出吗？");
            if (result == DialogResult.OK)
            {
                Application.ExitThread();
            }
        }

        private void button3_MouseHover(object sender, EventArgs e)
        {
            this.button3.ForeColor = Color.Red;
            Font font = new Font(this.button3.Font.FontFamily, 12f, this.button3.Font.Style);
            this.button3.Font = font;
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            this.button3.ForeColor = _color;
            this.button3.Font = _font;
        }

        #region 移动窗体
        /// <summary>
        /// 重写WndProc方法,实现窗体移动和禁止双击最大化
        /// </summary>
        /// <param name="m">Windows 消息</param>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x4e:
                case 0xd:
                case 0xe:
                case 0x14:
                    base.WndProc(ref m);
                    break;
                case 0x84://鼠标点任意位置后可以拖动窗体
                    this.DefWndProc(ref m);
                    if (m.Result.ToInt32() == 0x01)
                    {
                        m.Result = new IntPtr(0x02);
                    }
                    break;
                case 0xA3://禁止双击最大化
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
        #endregion

        #region 无边框拖动

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        internal static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        public const int MOUSEWHEEL = 0x020A;
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
        #endregion

    }
}